package jgl.vajra.operator.widget.manager.retrofit;

import jgl.vajra.operator.widget.bean.CacheConfigBean;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import retrofit2.Retrofit;

/**
 * Created by Administrator on 2018/10/28.
 */

public class RetrofitCacheManager {
    private TimeUnit mDefaultTimeUnit = TimeUnit.SECONDS;
    private Vector<Map> mVector;
    private Long mDefaultTime = 10L;

    private static class SingleHttpMethods {
        private static final RetrofitCacheManager INSTANCE = new RetrofitCacheManager();
    }

    //获取单例
    public static RetrofitCacheManager getInstance() {
        return RetrofitCacheManager.SingleHttpMethods.INSTANCE;
    }

    public RetrofitCacheManager addRetrofit(Retrofit retrofit) {
        try {

            Class cls = retrofit.getClass();
            Field field = cls.getDeclaredField("serviceMethodCache");
            field.setAccessible(true);
            if (mVector == null) {
                mVector = new Vector<>();
            }
            Map m = (Map) field.get(retrofit);
            mVector.add(m);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return this;
    }

    public CacheConfigBean getCacheTime(String url) {
     /*   CacheConfigInfo cacheConfig = new CacheConfigInfo();

        for (Map serviceMethodCache : mVector) {

            for (Object entry : serviceMethodCache.keySet()) {
                Object o = serviceMethodCache.get(entry);
                try {
                    Method m = (Method) entry;
                    Cache cache = m.getAnnotation(Cache.class);
                    if (cache != null) {
                        TimeUnit timeUnit = mDefaultTimeUnit;
                        if (cache.timeUnit() != TimeUnit.NANOSECONDS) {
                            timeUnit = cache.timeUnit();
                        }
                        long t = mDefaultTime;
                        if (cache.time() != -1) {
                            t = cache.time();
                        }
                        long tm = timeUnit.toSeconds(t);
                        cacheConfig.setTime(tm);
                        cacheConfig.setForceCacheNoNet(cache.forceCacheNoNet());
                        return cacheConfig;
                    } else {
                        return cacheConfig;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }}*/

        return null;
    }

}
